package com.dmall.modules.admin.service.impl;

import com.dmall.modules.admin.entity.AdminUser;
import com.dmall.modules.admin.mapper.AdminUserMapper;
import com.dmall.modules.admin.service.AdminUserService;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.ReUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author Chen 2017/12/6
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    private AdminUserMapper adminUserMapper;

    public List<AdminUser> getAll() {
        PageHelper.startPage(2,2);
        return adminUserMapper.myFindAll();
    }

    /**
     *根据用户名或者手机号或者邮箱查询查询用户
     * @param username
     * @return
     */
    @Override
    public AdminUser queryByUserName(String username) {
        Example example = new Example(AdminUser.class);
        Example.Criteria criteria = example.createCriteria();

        if(ReUtil.isMatch("((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$",username)){
            //手机号正则判断
            criteria.orEqualTo("mobile",username);
        }else if(ReUtil.isMatch("^[A-Za-z\\d]+([-_.][A-Za-z\\d]+)*@([A-Za-z\\d]+[-.])+[A-Za-z\\d]{2,4}$",username)){
            //邮箱
            criteria.orEqualTo("email",username);
        }else {
            criteria.orEqualTo("userName",username);
        }
        List<AdminUser> adminUsers = adminUserMapper.selectByExample(example);
        if (adminUsers==null || adminUsers.size()==0 || adminUsers.size()>1){
            return null;
        }
        return adminUsers.get(0);
    }
}
